package com.allways.dubbo.mdm.service.logiarea.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.allways.dubbo.mdm.beans.DataChangeLog;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import com.allways.dubbo.mdm.service.log.IDataChangeLogService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.logiarea.Logiarea;
import com.allways.dubbo.mdm.service.logiarea.ILogiareaService;

/**
 * <p>ClassName: ILogiareaService</p>
 * <p>Description: 物流运作区域Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-10-14</p>
 */
@Service("mdmLogiareaService")
public class LogiareaServiceImpl extends MdmBaseDaoImpl implements ILogiareaService {
	Log log = LogFactory.getLog(LogiareaServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
     @Autowired
    IDataChangeLogService dataChangeLogService;
    
    @Override
    public int addLogiarea(Logiarea logiarea) {
   		log.debug("For the new logiarea parameters:"+ logiarea.toJson());
   		validColumnLen(logiarea);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(logiarea.getLogiareaCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getLogiareaByCode(logiarea.getLogiareaCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        int cnt=insert("logiarea.insertLogiarea",logiarea);
        
        /*数据变更日志*/
        DataChangeLog log = new DataChangeLog();
        log.setDataType(DataChangeLog.DataType.logiarea.getName());
        log.setDataPk(logiarea.getLogiareaId().toString());
        log.setModifyType(DataChangeLog.OPER_TYPE_ADD);
        dataChangeLogService.addDataChangeLog(log);
        
        return cnt;
    }

    @Override
    public int updateLogiarea(Logiarea logiarea) {
    	log.debug("Update for the logiarea parameters:"+ logiarea.toJson());
    	validColumnLen(logiarea);
     	//无code的不能更新
        if(StringUtils.isEmpty(logiarea.getLogiareaCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        Logiarea oldLogiarea = getLogiareaByCode(logiarea.getLogiareaCode());
        if(null == oldLogiarea){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldLogiarea.setId(logiarea.getId());
		oldLogiarea.setLogisticsAreaCode(logiarea.getLogisticsAreaCode());
		oldLogiarea.setLogisticsAreaName(logiarea.getLogisticsAreaName());
		oldLogiarea.setOrgType(logiarea.getOrgType());
		oldLogiarea.setCompanyCode(logiarea.getCompanyCode());
		oldLogiarea.setDealerCode(logiarea.getDealerCode());
		oldLogiarea.setCountryCode(logiarea.getCountryCode());
		oldLogiarea.setLatitude(logiarea.getLatitude());
		oldLogiarea.setLongitude(logiarea.getLongitude());
		oldLogiarea.setDataSource(logiarea.getDataSource());
		oldLogiarea.setIsDisabled(logiarea.getIsDisabled());
		oldLogiarea.setCreatedBy(logiarea.getCreatedBy());
		oldLogiarea.setCreatedTime(logiarea.getCreatedTime());
		oldLogiarea.setUpdatedBy(logiarea.getUpdatedBy());
		oldLogiarea.setUpdatedTime(logiarea.getUpdatedTime());
        
        int cnt= update("logiarea.updateLogiarea",oldLogiarea);
        /*数据变更日志*/
        DataChangeLog log = new DataChangeLog();
        log.setDataType(DataChangeLog.DataType.Logiarea.getName());
        log.setDataPk(oldLogiarea.getLogiareaId().toString());
        log.setModifyType(DataChangeLog.OPER_TYPE_EDIT);
        dataChangeLogService.addDataChangeLog(log);
        
        return cnt;
    }

    @Override
    public int deleteLogiarea(Logiarea logiarea) {
    	log.debug("Delete for the logiarea parameters:"+ logiarea.toJson());
    	 
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(logiarea.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
     	//无code的不能删除
        if(StringUtils.isEmpty(logiarea.getLogiareaCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        Logiarea fullLogiarea = getLogiareaByCode(logiarea.getLogiareaCode());
        
        /* 数据变更日志 */
        DataChangeLog log = new DataChangeLog();
        log.setDataType(DataChangeLog.DataType.Logiarea.getName());
        log.setDataPk(fullLogiarea.getLogiareaId().toString());
        log.setModifyType(DataChangeLog.OPER_TYPE_DELETE);
        dataChangeLogService.addDataChangeLog(log);
        
        return delete("logiarea.deleteLogiarea",logiarea);
    }

    @Override
    public Logiarea getLogiareaByCode(String code) {
    	log.debug("Query for logiarea code:" + code);
        return queryForObject("logiarea.getLogiareaByCode",code);
    }
    @Override
    public long getLogiareaCount(Logiarea logiarea) {
    	log.debug("Query for logiarea count parameters:" + logiarea.toJson());
        return queryForObject("logiarea.getLogiareaCount",logiarea);
    }
   @Override
    public List<Logiarea> queryLogiareas(Logiarea logiarea, int firstRow, int lastRow) {
      	log.debug("Query for logiarea parameters:" + logiarea.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("logiarea",logiarea);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("logiarea.queryLogiareas", param);
    }
    
    /**
     * 验证字段长度
     * @param logiarea 需要验证的对象
     */
    private void validColumnLen(Logiarea logiarea){
		if(!StringUtils.isEmpty(logiarea.getId()) && logiarea.getId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiarea.getLogisticsAreaCode()) && logiarea.getLogisticsAreaCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logiarea.getLogisticsAreaName()) && logiarea.getLogisticsAreaName().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logiarea.getOrgType()) && logiarea.getOrgType().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiarea.getCompanyCode()) && logiarea.getCompanyCode().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(logiarea.getDealerCode()) && logiarea.getDealerCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logiarea.getCountryCode()) && logiarea.getCountryCode().length()>2){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{2},null);
        }
		if(!StringUtils.isEmpty(logiarea.getLatitude()) && logiarea.getLatitude().length()>25){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{25},null);
        }
		if(!StringUtils.isEmpty(logiarea.getLongitude()) && logiarea.getLongitude().length()>25){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{25},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(logiarea.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
